﻿using UnityEngine.UIElements;

namespace winS.UnityEditor.UIElement
{
    /// <summary>
    /// 文本动画框
    /// </summary>
    public class TextBox : TextElement
    {
        public static readonly float defaultAnimationDuration = 0.24f;

        private float lastTextBoxHeight;

        private bool isOpen = true;

        public TextBox(bool enableRichText = false)
        {
            name = "文本框";

            this.enableRichText = enableRichText;

            style.overflow = Overflow.Hidden;
            style.whiteSpace = WhiteSpace.Normal;

            style.minHeight = 0f;
            style.paddingLeft = 2;
            style.SetUnityFontDefinition(PackageAssets.FontSet.microsoftYahei);

            style.SetTransition("height", defaultAnimationDuration);
            RegisterCallback<TransitionEndEvent>(OnTransitionEndEvent);
        }

        /// <summary>
        /// 折叠
        /// </summary>
        public void Fold()
        {
            if (!isOpen) return;
            isOpen = false;
            style.height = lastTextBoxHeight = layout.height;
            style.height = 0f;

        }
        /// <summary>
        /// 打开
        /// </summary>
        public void Open()
        {
            if (isOpen) return;
            isOpen = true;
            style.height = lastTextBoxHeight;
        }

        private void OnTransitionEndEvent(TransitionEndEvent transitionEndEvent)
        {
            if (isOpen) style.height = new StyleLength(StyleKeyword.Auto);
        }
    }
}